package cn.com.jinhao.hotline.admin.utils;

//import cn.com.jinhao.hotline.admin.service.IMailService;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * 服务监控钉钉通知
 *
 * @author yinjihuan
 * @date 2018-10-11
 */
public class DingDingNotifier extends AbstractStatusChangeNotifier {

//    @Autowired
//    IMailService iMailService;

    @Value("${spring.boot.admin.notify.mail.to}")
    private String to;

    public DingDingNotifier(InstanceRepository repository) {
        super(repository);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        String serviceName = instance.getRegistration().getName();
        String serviceUrl = instance.getRegistration().getServiceUrl();
        String status = instance.getStatusInfo().getStatus();
        Map<String, Object> details = instance.getStatusInfo().getDetails();

        StringBuilder str = new StringBuilder();
        str.append("【" + serviceName + "】");
        str.append("【服务地址】" + serviceUrl);
        str.append("【状态】" + status);
        str.append("【详情】" + JsonUtils.toJson(details));
        System.err.println(str);
//        iMailService.sendSimpleMail(to, "主题:Spring Cloud Admin监控警告", str.toString());
        return Mono.fromRunnable(() -> {
            DingDingMessageUtil.sendTextMessage(str.toString());
        });
    }

}
